<?php

/*

Nursery (nursery.php)

*/

$rank_check = 1;
$page_title = "Nursery";
include "header.inc.php";

// RUN TASKS

$findTasks2Run   = mysql_query("SELECT * FROM pet_feeder WHERE feedat <= '$timestamp'");
while ($runTasks = mysql_fetch_array($findTasks2Run))
{
	if ($runTasks[item_type] == 1)
	{
		$column = "hunger";
		$value = 1;
	}
	if ($runTasks[item_type] == 2)
	{
		$column = "attitude";
		$value = 3;
	}

	mysql_query("UPDATE user_pets2 SET $column = $column + $value WHERE id = '$runTasks[petid]' AND game = '$runTasks[game]'");

	if (($runTasks[parts] > 1) OR ($runTasks[parts] == "i"))
	{
		if ($runTasks[parts] != "i")
		{
			$runTasks[parts] -= 1;
		}
		mysql_query("INSERT INTO usersitems2 (owner, item_id, parts_left, game) VALUES ('$runTasks[userid]', '$runTasks[itemid]', '$runTasks[parts]', '$runTasks[game]')");
	}
}

mysql_query("DELETE FROM pet_feeder WHERE feedat <= '$timestamp'");

// END TASKS

$yourItems      = mysql_query("SELECT id,item_id,parts_left FROM usersitems2 WHERE owner = '$userid'");
while ($getYourItems = mysql_fetch_array($yourItems))
{
	$getItem    = fetch("SELECT item_name,item_type FROM items2 WHERE id = '$getYourItems[item_id]'");
	if (($getItem[item_type] == "food") OR ($getItem[item_type] == "toy"))
	{
		$listItems .= "<option value=$getYourItems[id]>$getItem[item_name]";
	}
}

$yourPets       = mysql_query("SELECT id,species,name FROM user_pets2 WHERE owner = '$userid' AND adoption = '0'");
while ($getYourPets = mysql_fetch_array($yourPets))
{
	$getPet     = fetch("SELECT name FROM pets2 WHERE id = '$getYourPets[species]'");
	$listPets  .= "<option value=$getYourPets[id]>$getYourPets[name] the $getPet[name]";
}
echo $openHTML;

$findPage = fetch("SELECT * FROM game_pages WHERE page_title = '$page_title' AND game = '$game'");
if (!$findPage[id])
{

print "<p align=center><a href=item_helper.php?game=$game>Item Helper</A> | <a href=myitems.php?game=$game>My Items</a> | <a href=mygallery.php?game=$game>My Gallery</a> | <a href=myshop.php?game=$game>My Shop</a> | <a href=battle_items.php?game=$game>Equipped Items</a> | <a href=safety_box.php?game=$game>Safety Box</a></p>

<p>Tired of having to feed and play with your pets every day? Well, now you can set up a Nursery Schedule, just put the items in, close the lid, and tell it when to use the item! Simple as that :) Below is a list of items scheduled to be fed/played with! When the task is run it will take one part off the item and return it to your inventory, if there are no parts left it deletes the item. Each task will cost 15 $pointsVar to schedule</p> <p><b>You may only have 25 scheduled tasks at a time.</b></p>";

}
else
{
	print "$findPage[page_info]";
}

print "<form action=nursery.pro.php?game=$game method=post><p><b>Schedule a new task:</b><br>
Give <select name=item_name>$listItems</select> to <select name=pet_name>$listPets</select> in <input type=text name=feed_number size=4> <select name=feed_time><option value=minutes>Minutes<option value=hours>Hours<option value=days>Days</select> <input type=submit value=\"Schedule\"></p></form><p><table cellspacing=0 cellpadding=0 width=425><tr><td colspan=2><p>Item</p></td><td><p>Scheduled at:</p></td><td><p>Scheduled for:</p></td></tr>";

$x = 1;
$findTasks = mysql_query("SELECT * FROM pet_feeder WHERE userid = '$userid' ORDER BY feedat ASC");
while ($getTasks = mysql_fetch_array($findTasks))
{
	$getItem  = fetch("SELECT item_name FROM items2 WHERE id = '$getTasks[itemid]'");
	$getPet   = fetch("SELECT species,name FROM user_pets2 WHERE id = '$getTasks[petid]'");
	$getPet2  = fetch("SELECT name FROM pets2 WHERE id = '$getPet[species]'");

	$usedOn   = date("M j @ g:i:s a",$getTasks[feedat]);
	print "<tr><td><p>$x.</p></td><Td><p>$getItem[item_name]</p></td><td><p>$usedOn</p></td><td><p>$getPet[name] the $getPet2[name]</p></td></tr>";
	$x++;
}

print "</table>";

echo $closeHTML;

?>